
$(function() {
	validateRule();
    initPrid();
});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});


function save() {
	var closeInfo = $('#signupForm').serialize();
	$.ajax({
		cache : true,
		type : "POST",
		url : "/sales/order/forward",
		data : closeInfo, // 你的formid
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				parent.layer.msg(data.msg);
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i>";
	$("#signupForm").validate({
		rules : {
			content : {
				required : true
			}
		},
		messages : {
			content : {
				required : icon + "关闭原因不能为空"
			}
		}
	});
}


function onTypeSelected() {
    var type = $('#type').val();
    if (!type) {
        parent.layer.msg("工单类型选择错误");
        return;
	}
    $.ajax({
        cache : true,
        type : "POST",
        url : "/sales/orderCause/getAllWorkCause",
        data : {type:type},
        async : false,
        error : function(request) {
            alert("Connection error");
        },
        success : function(data) {
            if (data.code === 0) {
                var causeList = data.causeList;
                if (causeList) {
                    $('#prid').empty();
                    for (var i =0; i< causeList.length; i++) {
                        var item = causeList[i];
                        if (item) {
                            $('#prid').append("<option value='" + item.id + "'>" + item.cause + "</option>");
						}
                    }
                }
            }
        }
    });

}

function initPrid() {
    var type = $('#type').val();
    var pridVal = $('#pridVal').val();
    if (pridVal) {
    	pridVal = parseInt(pridVal);
	}
    if (!type) {
        parent.layer.msg("工单类型选择错误");
        return;
    }
    $.ajax({
        cache : true,
        type : "POST",
        url : "/sales/orderCause/getAllWorkCause",
        data : {type:type},
        async : false,
        error : function(request) {
            alert("Connection error");
        },
        success : function(data) {
            if (data.code === 0) {
                var causeList = data.causeList;
                if (causeList) {
                    $('#prid').empty();
                    for (var i =0; i< causeList.length; i++) {
                        var item = causeList[i];
                        if (item.id === pridVal) {
                            $('#prid').append("<option selected value='" + item.id + "'>" + item.cause + "</option>");
                        }else {
                            $('#prid').append("<option value='" + item.id + "'>" + item.cause + "</option>");
                        }

                    }
                }
            }
        }
    });
}